package com.lzj.demo.collection.map;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 输入一个字符串,统计字符数量,输出统计结果
 * Java 中九种 Map 的遍历方式
 * */
public class MapDemo {

    public static void main(String[] args) {
        output(count(inputString()));
    }

    /**
     * 输入一个字符串
     * */
    private static String inputString(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String line = sc.nextLine();
        return line;
    }

    /**
     * 使用Map统计字符数量
     * */
    private static Map<Character,Integer> count(String line){
        Map<Character,Integer> map = new HashMap<>();
        for(int i=0;i<line.length();i++) {
            char key = line.charAt(i);
            Integer value = map.get(key);
            if (Objects.isNull(value)) {
                map.put(key, 1);
            } else {
                value++;
                map.put(key, value);
            }
        }
        return map;
    }

    /**
     * 遍历Map输出统计结果
     * */
    private static void output(Map<Character,Integer> map){
        System.out.println(testMap1(map));
        System.out.println(testMap2(map));
        System.out.println(testMap3(map));
        System.out.println(testMap4(map));
        System.out.println(testMap5(map));
        System.out.println(testMap6(map));
        System.out.println(testMap7(map));
        System.out.println(testMap8(map));
        System.out.println(testMap9(map));
    }

    /**
     * Map遍历方式一: 通过 for 和 map.entrySet() 来遍历
     * */
    private static StringBuilder testMap1(Map<Character,Integer> map){
        StringBuilder sb = new StringBuilder();
        for(Map.Entry<Character,Integer> entry: map.entrySet()){
            sb.append(entry.getKey()).append("(").append(entry.getValue()).append(")");
        }
        return sb;
    }

    /**
     * Map遍历方式二: 通过 for， Iterator 和 map.entrySet() 来遍历
     * */
    private static StringBuilder testMap2(Map<Character,Integer> map){
        StringBuilder sb = new StringBuilder();
        for(Iterator<Map.Entry<Character,Integer>> iterator = map.entrySet().iterator();iterator.hasNext(); ){
            Map.Entry<Character,Integer> entry = iterator.next();
            sb.append(entry.getKey()).append("(").append(entry.getValue()).append(")");
        }
        return sb;
    }

    /**
     * Map遍历方式三: 通过 while， Iterator 和 map.entrySet() 来遍历
     * */
    private static StringBuilder testMap3(Map<Character,Integer> map){
        StringBuilder sb = new StringBuilder();
        Iterator<Map.Entry<Character,Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Character,Integer> entry = iterator.next();
            sb.append(entry.getKey()).append("(").append(entry.getValue()).append(")");
        }
        return sb;
    }

    /**
     * Map遍历方式四: 通过 for 和 map.keySet() 来遍历
     * */
    private static StringBuilder testMap4(Map<Character,Integer> map){
        StringBuilder sb = new StringBuilder();
        Set<Character> keySet = map.keySet();
        for(Character key : keySet){
            Integer value = map.get(key);
            sb.append(key).append("(").append(value).append(")");
        }
        return sb;
    }

    /**
     * Map遍历方式五: 通过 for，Iterator 和 map.keySet() 来遍历
     * */
    private static StringBuilder testMap5(Map<Character,Integer> map){
        StringBuilder sb = new StringBuilder();
        for (Iterator<Character> key = map.keySet().iterator(); key.hasNext(); ) {
            Character k = key.next();
            sb.append(k).append("(").append(map.get(k)).append(")");
        }
        return sb;
    }

    /**
     * Map遍历方式六: 通过 while，Iterator 和 map.keySet() 来遍历
     * */
    private static StringBuilder testMap6(Map<Character,Integer> map){
        StringBuilder sb = new StringBuilder();
        Iterator<Character> it = map.keySet().iterator();
        while (it.hasNext()) {
            Character key = it.next();
            sb.append(key).append("(").append(map.get(key)).append(")");
        }
        return sb;
    }

    /**
     * Map遍历方式七: Java 8 通过 map.forEach() 来遍历
     * */
    private static StringBuilder testMap7(Map<Character,Integer> map){
        StringBuilder sb = new StringBuilder();
        map.forEach((key, value) -> sb.append(key).append("(").append(value).append(")"));
        return sb;
    }

    /**
     * Map遍历方式八: Java 8 通过 Stream 来遍历
     * */
    private static StringBuilder testMap8(Map<Character,Integer> map){
        StringBuilder sb = new StringBuilder();
        map.entrySet().stream().map(entrySet->{
            sb.append(entrySet.getKey()).append("(").append(entrySet.getValue()).append(")");
            return sb.toString();
        }).collect(Collectors.toList());
        return sb;
    }

    /**
     * Map遍历方式九: Java 8 通过 ParallelStream 来遍历
     * */
    private static StringBuilder testMap9(Map<Character,Integer> map){
        StringBuilder sb = new StringBuilder();
        map.entrySet().parallelStream().map(entrySet->{
            sb.append(entrySet.getKey()).append("(").append(entrySet.getValue()).append(")");
            return sb.toString();
        }).collect(Collectors.toList());
        return sb;
    }
}
